package com.example.testdemo.image_progress_bar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.example.testdemo.R;

/**
 * Created by 颜 on 04.12.012.
 * 图片进度条
 */

public class ImageProgressBar extends View {
    private Paint paint;
    /**
     * 背景
     */
    private Bitmap backgroundBitmap;
    /**
     * 前景
     */
    private Bitmap prospectBitmap;

    private int progress;//进度
    private final int MAX_PROGRESS = 100;//最大进度
    private ViewGroup.LayoutParams params;

    private Rect rect;
    private RectF rectF;
    private Rect backRect;
    private RectF backRectF;

    public ImageProgressBar(Context context) {
        super(context);
    }

    public ImageProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initData(context, attrs);
    }


    public ImageProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 获取参数
     */
    private void initData(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ImageProgressBar);
        //取出背景图片资源ID
        int background = typedArray.getResourceId(R.styleable.ImageProgressBar_background_image, 0);
        //取出前景图片资源ID
        int prospect = typedArray.getResourceId(R.styleable.ImageProgressBar_prospect_image, 0);

        typedArray.recycle();//用完释放
        paint = new Paint();
        backgroundBitmap = BitmapFactory.decodeResource(getResources(), background);
        prospectBitmap = BitmapFactory.decodeResource(getResources(), prospect);
        rect = new Rect(0,0,0,backgroundBitmap.getHeight());
        rectF = new RectF(0,0,0,prospectBitmap.getHeight());
    }

    private void initBackgroundRect() {
        int width;
        int height;
        if (backgroundBitmap.getWidth() > params.width){
            width = params.width;
        }else {
            width = backgroundBitmap.getWidth();
        }
        if (backgroundBitmap.getHeight() > params.height){
            height = params.height;
        }else {
            height = backgroundBitmap.getHeight();
        }
        backRect = new Rect(0,0,width,height);
        backRectF = new RectF(0,0,width,height);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        params = getLayoutParams();
        initParams();
        initBackgroundRect();
    }

    /**
     * 设置适应屏幕
     */
    private void initParams() {
        if (params.height == ViewGroup.LayoutParams.WRAP_CONTENT){
            params.height = backgroundBitmap.getHeight();
            setLayoutParams(params);
        }
        if (params.width == ViewGroup.LayoutParams.WRAP_CONTENT){
            params.width = backgroundBitmap.getWidth();
            setLayoutParams(params);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //设置画笔
        paint.setAntiAlias(true);

        //绘制背景
        canvas.drawBitmap(backgroundBitmap, backRect, backRectF, paint);
        //绘制前景
        canvas.drawBitmap(prospectBitmap, rect, rectF, paint);
    }


    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        int width;
        if (prospectBitmap.getWidth() > backgroundBitmap.getWidth()){
            width = backgroundBitmap.getWidth();
        }else {
            width = prospectBitmap.getWidth();
        }
        int right = (int) (width * (progress / 100f));
        rect = new Rect(0,0,right,prospectBitmap.getHeight());
        rectF = new RectF(0,0,right,prospectBitmap.getHeight());
        postInvalidate();
    }

}
